# Grammar Testing
# Pigasus Language
# Philip Tjimos

cp ../src/grammar/pigasus.lpp ./
cp ../src/grammar/pigasus-grammar.ypp ./

# build the translator
flex -+ pigasus.lpp
bison pigasus-grammar.ypp
g++ pigasus-grammar.tab.cpp

# main function_definition
echo -n "Testing main... "
echo "void main() {" > test.pig
echo "int a;" >> test.pig 
echo "}" >> test.pig
./a.out < test.pig > result.cc
diff -bBws result.cc ./samples/main1.cc

echo -n "Testing main with input... "
echo "void main( list inputs ) {" > test.pig
echo "int a;" >> test.pig 
echo "}" >> test.pig
./a.out < test.pig > result.cc
diff -bBws result.cc ./samples/main2.cc

# other function_definitions
echo -n "Testing functions with all return values... "
echo "int foo( ) {}" > test.pig
echo "void foo2( ) {}" >> test.pig
echo "float foo3( ) {}" >> test.pig
echo "string foo4( ) {}" >> test.pig
echo "list foo5( ) {}" >> test.pig
echo "map foo6( ) {}" >> test.pig
echo "file foo7( ) {}" >> test.pig
./a.out < test.pig > result.cc
diff -bBws result.cc ./samples/type_func.cc

echo -n "Testing functions with different input... "
echo "int foo( int in ) {}" > test.pig
echo "void foo2( file in ) {}" >> test.pig
echo "float foo3( map in ) {}" >> test.pig
echo "string foo4( list in ) {}" >> test.pig
echo "list foo5( string in ) {}" >> test.pig
echo "map foo6( float in ) {}" >> test.pig
echo "file foo7( void in ) {}" >> test.pig
./a.out < test.pig > result.cc
diff -bBws result.cc ./samples/type_func1.cc

echo -n "Testing function with input list... "
echo "void foo ( void in1, int in2, float in3, string in4, list in5, map in6, file in7 ) {}" > test.pig
./a.out < test.pig > result.cc
diff -bBws result.cc ./samples/func_inputlist.cc

# job_definition
echo -n "Testing job definitions... "
echo "job foobar( list a ) {}" > test.pig
./a.out < test.pig > result.cc
diff -bBws result.cc ./samples/job.cc

# statements
echo "void main () {" > test.pig

echo "Testing initialization_statements... "
echo "string myString = \"hello\";" >> test.pig
echo "list myList = [string1, string2, string3];" >> test.pig
echo "map myMap = [name=>myName, pass=>myPass];" >> test.pig

echo "Testing matched_statements... "
echo "if ( i=0 ) { } else { }" >> test.pig

echo "Testing open_statements... "
echo "if ( i=0 ) { }" >> test.pig
echo "if ( i=0 ) { } else if ( i=0 ) { } else { }" >> test.pig

echo "Testing expression_statements... "
echo ";" >> test.pig
echo "int i;" >> test.pig
echo "int i=0;" >> test.pig

echo "Testing iteration_statements... "
echo "while ( i=0 ) { }" >> test.pig
echo "do {} while ( i=0 );" >> test.pig
echo "for (int i=0; i<1; ) { }" >> test.pig
echo "for (;;) { }" >> test.pig
echo "for (int i=0; i<1; i=i+1) { }" >> test.pig

echo "Testing jump_statements... "
echo "continue;" >> test.pig
echo "break;" >> test.pig
echo "return;" >> test.pig
echo "return i=0;" >> test.pig

echo "Testing push_statements... "
echo "int a = push foo, null, null, var;" >> test.pig
echo "push foo, var1, var2, var3;" >> test.pig

echo "Testing wait_statements... "
echo "wait var1;" >> test.pig
echo "waitall;" >> test.pig
echo "}" >> test.pig

echo -n "Result of all statements... "
./a.out < test.pig > result.cc
diff -bBws result.cc ./samples/statements.cc

# maps & lists

echo -n "Testing maps & lists... "
echo "void main() {" > test.pig
echo "list l = [a];" >> test.pig
echo "l = [a];" >> test.pig
echo "l = [a,b];" >> test.pig
echo "l = [a] + [b];" >> test.pig
echo "l = [ [ user=>user1, pass=>pass1], [user=>user2, pass=>pass2] ];" >> test.pig
echo "list l = [ [ user=>user1, pass=>pass1], [user=>user2, pass=>pass2] ];" >> test.pig
echo "l = a + b + [[a,b]] + [c] + d;" >> test.pig
echo "map m = [user => user1];" >> test.pig
echo "m = [user => user1] + [pass => pass1];" >> test.pig
echo "}" >> test.pig
./a.out < test.pig > result.cc
diff -bBws result.cc ./samples/list_maps.cc

# expressions
echo "Testing expressions... "
echo "void main() {" > test.pig
echo "a = 3;" >> test.pig
echo "a=3, b=4, c=5;" >> test.pig

echo "Testing assignment_expressions... "
echo "l = c = d;" >> test.pig
echo "l = [a] + [d] + [e];" >> test.pig
echo "l = [a=>a] + [b=>b] + [c=>c];" >> test.pig

echo "Testing logical_or_expressions... "
echo "if (a || b) { }" >> test.pig

echo "Testing logical_and_expressions... "
echo "if (a && b) { }" >> test.pig

echo "Testing equality_expressions... "
echo "if (a == b) { }" >> test.pig
echo "if (a != b) { }" >> test.pig

echo "Testing rational_expressions... "
echo "if (a < b ) { }" >> test.pig
echo "if (a > b ) { }" >> test.pig
echo "if (a <= b ) { }" >> test.pig
echo "if (a >= b ) { }" >> test.pig

echo "Testing additive_expressions... "
echo "if (a+b < 3) { }" >> test.pig
echo "if (a-b < 3) { }" >> test.pig

echo "Testing multiplicative_expressions... "
echo "if (a+b*3 < 4) { }" >> test.pig
echo "if (a-b/3 < 4) { }" >> test.pig
echo "a = \"test %s\" % myString;" >> test.pig
echo "a = \"test %s%s%s\" % [string1, string2, string3];" >> test.pig
echo "if (a mod b) { }" >> test.pig

echo "Testing cast_expressions... "
echo "int a = (int) b;" >> test.pig
echo "float a = (float) b;" >> test.pig
echo "map a = (map) b;" >> test.pig
echo "list a = (list) b;" >> test.pig
echo "file a = (file) b;" >> test.pig
echo "string a = (string) b;" >> test.pig

echo "Testing unary_expressions... "
echo "loadcsv b;" >> test.pig
echo "int b = length d;" >> test.pig
echo "print \"hello\";" >> test.pig
echo "connect server1;" >> test.pig

echo "Testing system_expressions... "
echo "@{\"run this on the shell\"};" >> test.pig

echo "Testing postfix_expressions... "
echo "int a = array[b];" >> test.pig
echo "int b = func(d);" >> test.pig
echo "int b = func(a,b,c,d);" >> test.pig
echo "int b = func(a=b, c=d);" >> test.pig
echo "} " >> test.pig

echo -n "Result of all expressions... "
./a.out < test.pig > result.cc
diff -bBws result.cc ./samples/expressions.cc

echo ""
echo "Pigasus Grammar Checks Completed"
echo ""

rm result.cc
rm pigMain.cc
rm test.pig
rm *.out
rm *.ypp
rm *.lpp
rm *.cpp
rm foobar.cc
rm lex.yy.cc
